package com.changfen.client.zset;


import com.changfen.client.util.RedisUtils;
import com.fasterxml.jackson.databind.util.JSONPObject;
import io.lettuce.core.*;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.changfen.client.constant.RedisKey.LANGUAGE;

/**
 * @author 86180
 * @Classname Rank
 * @Description TODO
 * @Date 2022/9/6 21:35
 */
@Slf4j
public class Rank {
    /**
     * 1.依次查润java go python c# 分数一次为90 80 23 70
     * 2.从高到低排序数据
     * 3.给C#加上30分，继续排序
     *
     * @param args
     */
    public static void main(String[] args) throws InterruptedException {
        RedisClient redisClient = RedisUtils.getRedisClient();
        StatefulRedisConnection<String, String> connection = RedisUtils.getConnection(redisClient);


        ScoredValue<String> value1 = ScoredValue.just(90.0, "java");
        ScoredValue<String> value2 = ScoredValue.just(80.0, "go");
        ScoredValue<String> value3 = ScoredValue.just(23.0, "python");
        ScoredValue<String> value4 = ScoredValue.just(70.0, "c#");


        RedisAsyncCommands<String, String> async = connection.async();

        RedisFuture<Long> zaddFuture = async.zadd(LANGUAGE, value1, value2, value3, value4);

        zaddFuture.thenRun(() -> {
            RedisFuture<List<String>> zrevrange = async.zrevrange(LANGUAGE, 0, 3);
            zrevrange.thenAccept(strings -> log.info("rank list = {}", strings));
        }).thenRun(()->{
            async.zincrby(  LANGUAGE,50.0,"c#").thenRun(()->{
                RedisFuture<List<String>> zrevrange = async.zrevrange(LANGUAGE, 0, 3);
                zrevrange.thenAccept(strings -> log.info("rank list = {}", strings));
            });
        });
        Thread.sleep(10000);
    }



}
